package com.aidex.biz.controller;

import com.aidex.biz.domain.LoanBill;
import com.aidex.biz.service.LoanBillService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 还款信息Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/biz/bizLoanBill")
public class LoanBillController extends BaseController
{
    @Autowired
    private LoanBillService loanBillService;

    /**
     * 查询还款信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:list')")
    @GetMapping("/list")
    public R<PageInfo> list(LoanBill loanBill, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                loanBill.setCompanyId(null);
        }else{
                     loanBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        loanBill.setPage(new PageDomain(request, response));
        return R.data(loanBillService.findPage(loanBill));
    }

    /**
     * 获取还款信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:query')")
    @GetMapping(value = "/{id}")
    public R<LoanBill> detail(@PathVariable("id") String id)
    {
        return R.data(loanBillService.get(id));
    }

    /**
     * 新增还款信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:add')")
    @Log(title = "还款信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  LoanBill loanBill)
    {
        return R.status(loanBillService.save(loanBill));
    }

    /**
     * 修改还款信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:edit')")
    @Log(title = "还款信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated LoanBill loanBill)
    {
        return R.status(loanBillService.save(loanBill));
    }


    /**
     * 删除还款信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:remove')")
    @Log(title = "还款信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(loanBillService.deleteLoanBillByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(loanBillService.findMaxSort(new LoanBill()));
    }

    /**
     * 导出还款信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizLoanBill:export')")
    @Log(title = "还款信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(LoanBill loanBill)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    loanBill.setCompanyId(null);
            }else{
                         loanBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<LoanBill> list = loanBillService.findList(loanBill);
        ExcelUtil<LoanBill> util = new ExcelUtil<LoanBill>(LoanBill.class);
        return util.exportExcel(list, "还款信息数据");
    }

}
